package com.cskaoyan.controller;

import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.WxCartCheckoutVo;
import com.cskaoyan.bean.vo.WxCartIndexVo;
import com.cskaoyan.service.WxCartService;

import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;


/**
 * @description:
 * @author: Tao
 * @time: 2022/12/31 23:46
 */

@RestController
@RequestMapping("wx/cart")
public class WxCartController {
    @Autowired
    WxCartService wxCartService;


    /**
     * 完成购物车的查询
     *
     * @return
     */
    @RequestMapping("index")
    public BaseRespVo index() {
        WxCartIndexVo wxCartIndexVo = wxCartService.getCartIndex();
        return BaseRespVo.ok(wxCartIndexVo, "成功");
    }


    /**
     * 完成购物车中商品数量的查询
     *
     * @return 购物车的中的商品数量
     */
    @RequestMapping("goodscount")
    public BaseRespVo getgoodscount() {
        int goodscount = wxCartService.getGoodsCount();
        return BaseRespVo.ok(goodscount, "成功");
    }

    /**
     * 完成购物车中添加
     *
     * @param wxCartAddBo
     * @return 目前购物车中商品的数量
     */

    @RequestMapping("add")
    public BaseRespVo addCart(@RequestBody WxCartAddBo wxCartAddBo) {
        //执行插入操作
        wxCartService.addCart(wxCartAddBo);
        //执行查找购物车中数量的操作
        int goodscount = wxCartService.getGoodsCount();
        return BaseRespVo.ok(goodscount, "成功");
    }

    /**
     * 执行购物车的选择功能
     *
     * @param checkedBo
     * @return
     */
    @RequestMapping("checked")
    public BaseRespVo checked(@RequestBody WxCartCheckedBo checkedBo) {
        WxCartIndexVo wxCartIndexVo = wxCartService.checked(checkedBo);
        return BaseRespVo.ok(wxCartIndexVo, "成功");
    }

    /**
     * 完成购物车的删除功能
     *
     * @param wxCartDeleteBo
     * @return
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody WxCartDeleteBo wxCartDeleteBo) {
        WxCartIndexVo wxCartIndexVo = wxCartService.delete(wxCartDeleteBo);
        return BaseRespVo.ok(wxCartIndexVo, "成功");
    }


    /**
     * 修改操作
     *
     * @param cartUpdateBo
     * @return
     */
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody WxCartUpdateBo cartUpdateBo) {
        wxCartService.update(cartUpdateBo);
        return BaseRespVo.ok("成功");
    }

    /**
     * 快速添加操作
     *
     * @param wxCartFastAddBo
     * @return
     */
    @RequestMapping("fastadd")
    public BaseRespVo fastAdd(@RequestBody WxCartFastAddBo wxCartFastAddBo) {
        Integer data = wxCartService.fastAdd(wxCartFastAddBo);
        return BaseRespVo.ok(data, "成功");
    }

    /**
     * 购物车付款的检查功能
     *
     * @param wxCartCheckoutBo
     * @return
     */
    @RequestMapping("checkout")
    public BaseRespVo checkout(WxCartCheckoutBo wxCartCheckoutBo, HttpSession session) {
        //向session中放入数据
        session.setAttribute("cartId", wxCartCheckoutBo.getCartId());
        session.setAttribute("addressId", wxCartCheckoutBo.getAddressId());
        //
        session.setAttribute("couponId", wxCartCheckoutBo.getCouponId());
        session.setAttribute("userCouponId", wxCartCheckoutBo.getUserCouponId());
        session.setAttribute("grouponRulesId", wxCartCheckoutBo.getGrouponRulesId());

        WxCartCheckoutVo wxCartCheckoutVo = wxCartService.checkout(wxCartCheckoutBo);
        // add by LA 2023/01/23 1:28
        // 订单总费用
        session.setAttribute("totalPrice",wxCartCheckoutVo.getOrderTotalPrice());
        // 订单 实际总金额
        session.setAttribute("actualPrice", wxCartCheckoutVo.getActualPrice());
        // 运费
        session.setAttribute("freightPrice", wxCartCheckoutVo.getFreightPrice());
        // 优惠券数目
        // 优惠券的金额  couponPrice
        session.setAttribute("couponPrice", wxCartCheckoutVo.getCouponPrice());
        // 商品规格详情
        session.setAttribute("checkedGoodsList", wxCartCheckoutVo.getCheckedGoodsList());
        // 地址对象
        session.setAttribute("address",wxCartCheckoutVo.getCheckedAddress());

        return BaseRespVo.ok(wxCartCheckoutVo, "成功");
    }
}
